package com.ucampus.service;

import com.ucampus.dao.BuildingDao;
import com.ucampus.entity.Building;
import com.ucampus.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class BuildingService {
    @Autowired
    private BuildingDao buildingDao;

    public ResponseData getAll() {
        ResponseData responseData = new ResponseData();
        List<Building> buildings = buildingDao.getAll();
        if(!buildings.isEmpty()) {
            responseData.setData(buildings);
            responseData.setMessage("查询成功");
        }else {
            responseData.setMessage("查询失败，请重试");
        }
        return responseData;
    }


    public ResponseData getByName(String name) {
        ResponseData responseData = new ResponseData();
        List<Building> buildings = buildingDao.getByName(name);
        if(!buildings.isEmpty()) {
            responseData.setData(buildings);
            responseData.setMessage("查询成功");
        }else {
            responseData.setMessage("查询失败，换个名试试");
        }
        return responseData;
    }

    public ResponseData add(String name, String introduction) throws IOException {
        ResponseData responseData = new ResponseData();
        int n = buildingDao.add(name,introduction);
        if(n > 0) {
            responseData.setMessage("添加成功");
        }else {
            responseData.setMessage("添加失败，请重试");
        }
        return responseData;
    }

    public ResponseData update(int id,String name, String introduction) throws IOException {
        ResponseData responseData = new ResponseData();
        int n = buildingDao.update(id,name,introduction);
        if(n > 0) {
            responseData.setMessage("修改成功");
        }else {
            responseData.setMessage("修改失败，请重试");
        }
        return responseData;
    }

    public ResponseData delete(List<Integer> list) {
        ResponseData responseData = new ResponseData();
        int n = buildingDao.delete(list);
        if(n > 0) {
            responseData.setMessage("删除成功"+n+"条数据已删除，请刷新页面获取最新数据");
        }else {
            responseData.setMessage("删除失败，请重试");
        }
        return responseData;
    }

    public byte[] getImage(int id) {
        return buildingDao.getImage(id);
    }
}
